<template>
	<view class="download_apk">
		<view class="download_apk_box">
			<view class="download_apk_title">
				正在下载更新中，请勿关闭
			</view>
			<view class="download_apk_loading_box">
				<view class="download_apk_loading">
					<view class="download_apk_bar" :style="{width:downloadCount}"></view>
				</view>
				<text class="download_apk_count">{{downloadCount}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				downloadCount: '0%'
			};
		},
		methods: {
			downloadApk(res) {
				let _this = this
				var url = res.url; // 下载文件地址
				function onStateChanged(download, status) {
					let {
						totalSize,
						downloadedSize
					} = download;
					if (download.state == 3) _this.downloadCount = `${parseInt((downloadedSize / totalSize) * 100)}%`
					// if (download.state == 4 && status == 200) {
					// 	// 下载完成 
					// 	console.log("Download success: " + download.getFileName());
					// }
				}
				var dtask = plus.downloader.createDownload(url, {}, function(d, status) {
					if (status == 200) { // 下载成功
						var path = d.filename;
						plus.runtime.install(path);
					} else { //下载失败
						alert("Download failed: " + status);
					}
				});
				dtask.addEventListener("statechanged", onStateChanged, false);
				dtask.start();
			}
		}
	}
</script>

<style lang="less">
	.download_apk {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 9999;
		background: rgba(0, 0, 0, .6);
		transition: background 0.3s linear;

		.download_apk_box {
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -30%);
			width: 500upx;
			height: 200upx;
			background: #FFFFFF;
			padding: 50upx;
		}

		.download_apk_title {
			font-size: 35upx;
			color: #333;
		}

		.download_apk_loading_box {
			margin-top: 80upx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10upx;

			.download_apk_loading {
				width: 350upx;
				height: 20upx;
				background: #999;
				border-radius: 10upx;
				overflow: hidden;

				.download_apk_bar {
					height: 20upx;
					background: #00BFFF;
				}
			}

			.download_apk_count {
				flex: 1;
				text-align: right;
				font-size: 24upx;
				color: #333;
			}
		}
	}
</style>
